﻿using H.RT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Windows.Forms;
using static H.Runtime.Host.NativeMethods;



namespace H.Runtime.Host
{
    public class Program
    {
        #region 注入启动函数
        public static int Main(string args)
        {
            string[] piece0 = args.Split('#');
            string appBase = string.IsNullOrWhiteSpace(piece0[0]) ? AppDomain.CurrentDomain.SetupInformation.ApplicationBase : piece0[0];

            HDomain.InitEnvironment(appBase);

            try
            {
                HRemote hRemote = new HRemote();
                foreach (string exe in piece0.Skip(1))
                {
                    if (!string.IsNullOrEmpty(exe))
                    {
                        string[] appPiece = exe.Split('|');
                        hRemote.ExecuteApps(appPiece[0], appPiece.Skip(1).ToArray());
                    }
                }
                hRemote.Start();
            }
            catch (SocketException err)
            {
                if (err.SocketErrorCode == SocketError.AddressAlreadyInUse)
                {
                    MessageBox.Show("HRT已经在运行当中,请不要重复运行");
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
            return 0;
        }

        #endregion

        #region 混淆代码(一个小游戏)

        private static readonly string[,] map = new string[20, 20];
        private static readonly List<Enemy> enemyList = new List<Enemy>();
        private static readonly List<Chest> chestList = new List<Chest>();
        private static string msgbox = "";
        private static bool isBattle = false, isDead = false;
        private static Enemy tarEnemy = null;
        private const int BOSS_LEVEL = 10;
        private static Enemy boss = null;
        private static int level = 0, enemyDizzy = 0, battleRound = 0;

        public static int Main(string[] args)
        {
            AllocConsole();
            Console.Title = "恶魔地牢";
            Console.SetWindowSize(77, 30);
            Console.SetBufferSize(77, 30);

            Console.CursorVisible = false;
            Start();
            while (true)
            {
                Update();
            }
        }
        private static void Start()
        {
            Player.Reset();
            isDead = false;
            isBattle = false;
            tarEnemy = null;
            boss = new Enemy(0, 0, true);
            level = 0;
            battleRound = 0;
            enemyDizzy = 0;
            msgbox = "欢迎来到恶魔地牢  (方向键移动)\n★ = 玩家\nθ = 道具\n︾ = 下一层\n<请按键分配属性点>\n1：力量 2：防御 3：敏捷 4：智力\n";
            MapGenerator();
        }
        private static void Update()
        {
            PrintMap();
            StatusBar();
            MsgboxBar();
            BattleBar();
            Info();
            if (isDead)
            {
                ConfirmDead();
            }
            else if (boss == null)
            {
                MissionCompeleted();
            }
            else if (isBattle)
            {
                Battle(tarEnemy);
            }
            else
            {
                EnemyController();
                PlayerController();
            }
            Console.Clear();
        }
        private static Enemy GetTargetEnemy(int x, int y, bool isBoss = false)
        {
            if (isBattle)
            {
                return null;
            }

            msgbox += "进入战斗\n";
            if (isBoss)
            {
                isBattle = true;
                battleRound = 0;
                enemyDizzy = 0;
                return boss;
            }
            for (int i = 0; i < enemyList.Count; i++)
            {
                Enemy enemy = enemyList[i];
                if (enemy.x == x && enemy.y == y)
                {
                    isBattle = true;
                    battleRound = 0;
                    enemyDizzy = 0;
                    return enemy;
                }
            }
            return null;
        }
        private static void Battle(Enemy enemy)
        {
            const int ATTACK = 0, SKILL1 = 1, SKILL2 = 2, SKILL3 = 3, ESCAPE = 4;
            int action = ATTACK;
        c:
            ConsoleKey key = Console.ReadKey(true).Key;
            switch (key)
            {
                case ConsoleKey.Spacebar:
                    action = ATTACK;
                    break;
                case ConsoleKey.Q:
                    if (CanSpell(1) == "")
                    {
                        action = SKILL1;
                    }
                    else
                    {
                        msgbox += CanSpell(1);
                        MsgboxBar();
                        goto c;
                    }
                    break;
                case ConsoleKey.W:
                    if (CanSpell(2) == "")
                    {
                        action = SKILL2;
                    }
                    else
                    {
                        msgbox += CanSpell(2);
                        MsgboxBar();
                        goto c;
                    }
                    break;
                case ConsoleKey.E:
                    if (CanSpell(3) == "")
                    {
                        action = SKILL3;
                    }
                    else
                    {
                        msgbox += CanSpell(3);
                        MsgboxBar();
                        goto c;
                    }
                    break;
                case ConsoleKey.R:
                    action = ESCAPE;
                    break;
                default:
                    goto c;
            }
            battleRound++;
            msgbox += "<回合 " + battleRound + ">\n";
            MsgboxBar();
            //玩家动作
            switch (action)
            {
                case ATTACK:
                    int dmg = enemy.GetHurt(Player.atk, Player.dex);
                    switch (dmg)
                    {
                        case -1:
                            msgbox += enemy.name + " 闪避了攻击\n";
                            break;
                        case 0:
                            msgbox += enemy.name + " 格挡了伤害\n";
                            break;
                        default:
                            msgbox += enemy.name + " 受到 " + dmg + " 伤害\n";
                            break;
                    }
                    break;
                case SKILL1:
                    dmg = Event.rnd.Next(4, 7) + Player.Int;
                    msgbox += "施放<火球术> " + enemy.name + " 受到 " + dmg + " 伤害\n";
                    MsgboxBar();
                    enemy.life -= dmg;
                    Player.mana -= 3;
                    break;
                case SKILL2:
                    dmg = Event.rnd.Next(8, 13) + Player.Int * 3;
                    msgbox += "施放<治愈术> 回复 " + dmg + " 生命\n";
                    Player.life += dmg;
                    Player.mana -= 5;
                    break;
                case SKILL3:
                    dmg = Event.rnd.Next(1, 3) + Player.Int;
                    msgbox += "施放<眩晕术> " + enemy.name + " 眩晕 " + (dmg + 1) + " 回合\n";
                    MsgboxBar();
                    Player.mana -= 4;
                    enemyDizzy = dmg;
                    return;
                case ESCAPE:
                    if (Event.rnd.Next(0, 2) == 0)
                    {
                        isBattle = false;
                        msgbox += "<逃跑成功>\n";
                        return;
                    }
                    else
                    {
                        msgbox += "<逃跑失败>\n";
                    }
                    break;
            }
            MsgboxBar();
            //msgbox += "> ";
            if (enemy.life > 0)
            {
                if (enemyDizzy == 0)
                {
                    int dmg = Player.GetHurt(enemy.atk, enemy.dex);
                    switch (dmg)
                    {
                        case -1:
                            msgbox += "你闪避了攻击\n";
                            break;
                        case 0:
                            msgbox += "你格挡了伤害\n";
                            break;
                        default:
                            if (Player.life > 0)
                            {
                                msgbox += "你受到 " + dmg + " 伤害\n";
                            }
                            else
                            {
                                Player.life = 0;
                                msgbox += "你死了\n(按R键重新开始)\n";
                                MsgboxBar();
                                isDead = true;
                                return;
                            }
                            break;
                    }
                }
                else
                {
                    msgbox += enemy.name + " 眩晕中\n";
                    enemyDizzy--;
                }
            }
            else
            {

                msgbox += "敌人死亡\n";
                //如果敌人是BOSS
                if (enemy == boss)
                {
                    boss = null;
                    isBattle = false;
                }
                Player.exp += enemy.exp;
                if (Player.exp >= Player.lv * 2)
                {
                    msgbox += "<等级提升> (获得 1 属性点)\n";
                    MsgboxBar();
                    Player.exp -= Player.lv * 2;
                    Player.lv++;
                    Player.life += 8;
                    Player.mana += 8;
                    Player.ap++;
                }
                enemyList.Remove(enemy);
                isBattle = false;
            }
        }
        private static bool isHeight(int _val)
        {
            for (int i = 0; i < 4; i++)
            {
                switch (i)
                {
                    case 0:
                        if (_val < Player.atk)
                        {
                            return false;
                        }

                        break;
                    case 1:
                        if (_val < Player.def)
                        {
                            return false;
                        }

                        break;
                    case 2:
                        if (_val < Player.dex)
                        {
                            return false;
                        }

                        break;
                    case 3:
                        if (_val < Player.Int)
                        {
                            return false;
                        }

                        break;
                }
            }
            return true;
        }
        private static void StatusBar()
        {
            Console.SetCursorPosition(41, 0);
            Console.WriteLine("┌───────状态───────┐");
            Console.SetCursorPosition(41, 1);
            Console.WriteLine("│等级：    经验：    点数：    │");
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(49, 1); Console.Write(Player.lv);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(60, 1); Console.Write(Player.exp);
            Console.ForegroundColor = Player.ap > 0 ? ConsoleColor.Yellow : ConsoleColor.DarkYellow;
            Console.SetCursorPosition(71, 1); Console.Write(Player.ap);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.SetCursorPosition(41, 2);
            Console.WriteLine("│生命：    力量：    防御：    │");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(49, 2); Console.Write(Player.life);
            Console.ForegroundColor = isHeight(Player.atk) ? ConsoleColor.Red : ConsoleColor.DarkRed;
            Console.SetCursorPosition(60, 2); Console.Write(Player.atk);
            Console.ForegroundColor = isHeight(Player.def) ? ConsoleColor.Yellow : ConsoleColor.DarkYellow;
            Console.SetCursorPosition(71, 2); Console.Write(Player.def);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.SetCursorPosition(41, 3);
            Console.WriteLine("│法力：    敏捷：    智力：    │");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(49, 3); Console.Write(Player.mana);
            Console.ForegroundColor = isHeight(Player.dex) ? ConsoleColor.Magenta : ConsoleColor.DarkMagenta;
            Console.SetCursorPosition(60, 3); Console.Write(Player.dex);
            Console.ForegroundColor = isHeight(Player.Int) ? ConsoleColor.Cyan : ConsoleColor.DarkCyan;
            Console.SetCursorPosition(71, 3); Console.Write(Player.Int);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.SetCursorPosition(41, 4);
            Console.WriteLine("└────────────────┘");
            Console.WriteLine();
        }
        private static void BattleBar()
        {
            if (!isBattle)
            {
                return;
            }

            int i = 5, l = 41;
            Console.SetCursorPosition(41, i);
            Console.WriteLine("┌──战斗──┐"); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│敌人：      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.name); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│生命：      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.life); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│攻击：      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.atk); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│防御：      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.def); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│敏捷：      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.dex); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("└──────┘");
            Console.WriteLine();
            //操作
            i = 5; l = 57;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("┌───操作───┐"); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│空格键：<攻击>  │"); i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│技能 Q：        │"); Console.SetCursorPosition(l + 10, i);
            if (Player.skill1)
            {
                Console.Write("<火球术>");
            }
            else
            {
                Console.Write("<无>");
            }

            i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│技能 W：        │"); Console.SetCursorPosition(l + 10, i);
            if (Player.skill2)
            {
                Console.Write("<治愈术>");
            }
            else
            {
                Console.Write("<无>");
            }

            i++;
            Console.SetCursorPosition(l, i);
            Console.WriteLine("│道具 E：        │"); Console.SetCursorPosition(l + 10, i);
            if (Player.skill3)
            {
                Console.Write("<眩晕术>");
            }
            else
            {
                Console.Write("<无>");
            }

            i++;
            Console.SetCursorPosition(l, i); i++;
            Console.WriteLine("│按键 R：<逃跑>  │");
            Console.SetCursorPosition(l, i);
            Console.WriteLine("└────────┘");
            Console.WriteLine();
        }
        private static void MsgboxBar()
        {
            Console.SetCursorPosition(41, 12);
            Console.WriteLine("┌───────信息───────┐");
            for (int i = 1; i < 7; i++)
            {
                Console.SetCursorPosition(41, 12 + i);
                Console.WriteLine("│                                │");
            }
            Console.SetCursorPosition(41, 19);
            Console.WriteLine("└────────────────┘");
            int t = 0;
            for (int i = 0; i < msgbox.Length; i++)
            {
                i = msgbox.IndexOf("\n", i);
                if (i != -1)
                {
                    t++;
                }
            }
            //1\n2\n3\n4\n5\n6\n
            if (t > 6)
            {
                msgbox = msgbox.Substring(msgbox.IndexOf("\n") + 1, msgbox.Length - msgbox.IndexOf("\n") - 1);
            }
            string s = "";
            int p0 = 0, p1 = 0;
            for (int i = 0; i < 6; i++)
            {
                p1 = msgbox.IndexOf("\n", p0);
                if (p1 == -1)
                {
                    break;
                }

                s = p1 - p0 > 20 ? msgbox.Substring(p0, 19) + "..." : msgbox.Substring(p0, p1 - p0);
                p0 = p1 + 1;
                Console.SetCursorPosition(43, 13 + i);
                if (s.Contains("欢迎来到") || s.Contains("按R键") || s.Contains("恭喜通关"))
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                }
                else if (s.Contains("★"))
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }
                else if (s.Contains("θ") || s.Contains("等级提升"))
                {
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                }
                else if (s.Contains("︾"))
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                }
                else if (s.Contains("进入战斗") || s.Contains("你死了") || s.Contains("您已击败恶魔"))
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                }
                else if (s.Contains("发现"))
                {
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                Console.WriteLine(s);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }
        private static void ConfirmDead()
        {
        c:
            ConsoleKey key = Console.ReadKey(true).Key;
            if (key == ConsoleKey.R)
            {
                Start();
            }
            else
            {
                goto c;
            }
        }
        private static void MissionCompeleted()
        {
            msgbox += "恭喜通关\n您已击败恶魔，并成为了新的恶魔！\n";
            MsgboxBar();
            MsgboxBar();
            MsgboxBar();
        c:
            ConsoleKey key = Console.ReadKey(true).Key;
            if (key == ConsoleKey.R)
            {
                Start();
            }
            else
            {
                goto c;
            }
        }
        private static void PlayerController()
        {
        c:
            int x = Player.x, y = Player.y;
            ConsoleKey key = Console.ReadKey(true).Key;
            switch (key)
            {
                case ConsoleKey.UpArrow:
                    y--;
                    break;
                case ConsoleKey.DownArrow:
                    y++;
                    break;
                case ConsoleKey.LeftArrow:
                    x--;
                    break;
                case ConsoleKey.RightArrow:
                    x++;
                    break;
                case ConsoleKey.D1:
                    AddAp(1); goto c;
                case ConsoleKey.D2:
                    AddAp(2); goto c;
                case ConsoleKey.D3:
                    AddAp(3); goto c;
                case ConsoleKey.D4:
                    AddAp(4); goto c;
                default:
                    goto c;
            }
            //越界判断
            if (x < 1 && x < map.GetLength(0) - 1 && y < 1 && y < map.GetLength(1) - 1)
            {
                return;
            }
            //碰撞判断
            Object obj = HitTest(x, y);
            switch (obj)
            {
                case Object.Wall:
                    //msgbox += "无作为\n";
                    goto c;
                case Object.Enemy:
                    //msgbox += "【进入战斗】\n";
                    tarEnemy = GetTargetEnemy(x, y);
                    break;
                case Object.Chest:
                    OpenChest(x, y);
                    break;
                case Object.Boss:
                    tarEnemy = GetTargetEnemy(0, 0, true);
                    break;
                case Object.Exit:
                    MapGenerator();
                    break;
                default:
                    msgbox += "玩家移动\n";
                    Player.x = x;
                    Player.y = y;
                    break;
            }
        }
        private static void EnemyController()
        {
            for (int i = 0; i < enemyList.Count; i++)
            {
                Enemy enemy = enemyList[i];
                //移动部分
                int x = enemy.x, y = enemy.y;
                int r = Event.rnd.Next(3, 8);
                for (int _y = y - r; _y <= y + r; _y++)
                {
                    for (int _x = x - r; _x <= x + r; _x++)
                    {
                        //如果玩家在视野内
                        if (_x == Player.x && _y == Player.y)
                        {
                            //优先上下
                            if (Event.rnd.Next(0, 2) == 0)
                            {
                                if (Player.y < y)
                                {
                                    y--;
                                }
                                else if (Player.y > y)
                                {
                                    y++;
                                }
                                else if (Player.x < x)
                                {
                                    x--;
                                }
                                else if (Player.x > x)
                                {
                                    x++;
                                }
                            }
                            //优先左右
                            else
                            {
                                if (Player.x < x)
                                {
                                    x--;
                                }
                                else if (Player.x > x)
                                {
                                    x++;
                                }
                                else if (Player.y < y)
                                {
                                    y--;
                                }
                                else if (Player.y > y)
                                {
                                    y++;
                                }
                            }
                            goto c;
                        }
                    }
                }
                int dir = Event.rnd.Next(0, 4);
                switch (dir)
                {
                    case 0:
                        y--;
                        break;
                    case 1:
                        y++;
                        break;
                    case 2:
                        x--;
                        break;
                    case 3:
                        x++;
                        break;
                }
            c:
                Object obj = HitTest(x, y);
                switch (obj)
                {
                    case Object.Wall:
                        break;
                    case Object.Enemy:
                        break;
                    case Object.Player:
                        break;
                    case Object.Chest:
                        break;
                    case Object.Boss:
                        break;
                    case Object.Exit:
                        break;
                    default:
                        enemy.x = x;
                        enemy.y = y;
                        break;
                }
            }
        }
        private static void AddAp(int att)
        {
            if (Player.ap <= 0)
            {
                return;
            }

            Player.ap--;
            switch (att)
            {
                case 1:
                    Player.atk++;
                    break;
                case 2:
                    Player.def++;
                    break;
                case 3:
                    Player.dex++;
                    break;
                case 4:
                    Player.Int++;
                    break;
            }
            msgbox += "<剩余属性点 " + Player.ap + " >\n";
            StatusBar();
            MsgboxBar();
        }
        private static string CanSpell(int skill)
        {
            string msg = "";
            switch (skill)
            {
                case 1:
                    if (Player.skill1)
                    {
                        if (Player.mana < 3)
                        {
                            msg = "<法力不足>\n";
                        }
                    }
                    else
                    {
                        msg = "<尚未习得该技能>\n";
                    }

                    break;
                case 2:
                    if (Player.skill2)
                    {
                        if (Player.mana < 5)
                        {
                            msg = "<法力不足>\n";
                        }
                    }
                    else
                    {
                        msg = "<尚未习得该技能>\n";
                    }

                    break;
                case 3:
                    if (Player.skill3)
                    {
                        if (Player.mana < 4)
                        {
                            msg = "<法力不足>\n";
                        }
                    }
                    else
                    {
                        msg = "<尚未习得该技能>\n";
                    }

                    break;
            }
            return msg;
        }
        private static void OpenChest(int x, int y)
        {
            for (int i = 0; i < chestList.Count; i++)
            {
                Chest chest = chestList[i];
                if (chest.x == x && chest.y == y)
                {
                    msgbox += "发现 " + chest.treasure + " ";
                    switch (chest.treasure)
                    {
                        case "生命药水":
                            int val = Event.rnd.Next(5, 11);
                            msgbox += "增加 " + val + " 生命";
                            Player.life += val;
                            break;
                        case "法力药水":
                            val = Event.rnd.Next(5, 11);
                            msgbox += "增加 " + val + " 法力";
                            Player.mana += val;
                            break;
                        case "短剑":
                            val = Event.rnd.Next(1, 4);
                            msgbox += "增加 " + val + " 力量";
                            Player.atk += val;
                            break;
                        case "盔甲":
                            val = Event.rnd.Next(1, 3);
                            msgbox += "增加 " + val + " 防御";
                            Player.def += val;
                            break;
                        case "火球术卷轴":
                            msgbox += "学会了 火球术";
                            Player.skill1 = true;
                            break;
                        case "治愈术卷轴":
                            msgbox += "学会了 治愈术";
                            Player.skill2 = true;
                            break;
                        case "眩晕术卷轴":
                            msgbox += "学会了 眩晕术";
                            Player.skill3 = true;
                            break;
                    }
                    msgbox += "\n";
                    chestList.Remove(chest);
                }
            }
        }
        private enum Object
        {
            Ground, Wall, Enemy, Player, Chest, Boss, Exit
        };
        private static Object HitTest(int x, int y)
        {
            if (map[x, y] == "□")
            {
                return Object.Wall;
            }
            if (x == map.GetLength(0) - 2 && y == map.GetLength(1) - 1)
            {
                return Object.Exit;
            }
            //敌人
            for (int i = 0; i < enemyList.Count; i++)
            {
                Enemy enemy = enemyList[i];
                if (x == enemy.x && y == enemy.y)
                {
                    return Object.Enemy;
                }
            }
            //宝箱
            for (int i = 0; i < chestList.Count; i++)
            {
                Chest chest = chestList[i];
                if (x == chest.x && y == chest.y)
                {
                    return Object.Chest;
                }
            }
            if (x == Player.x && y == Player.y)
            {
                return Object.Player;
            }
            //BOSS
            if (level == BOSS_LEVEL)
            {
                if (x >= 8 && x <= 11 && y >= 8 && y <= 11)
                {
                    if (!(x == 8 && y == 8) && !(x == 11 && y == 8) && !(x == 11 && y == 9) && !(x == 11 && y == 11))
                    {
                        return Object.Boss;
                    }
                }
            }
            return Object.Ground;
        }
        private static void PrintMap()
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                for (int x = 0; x < map.GetLength(0); x++)
                {
                    if (x == Player.x && y == Player.y)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("★");
                        continue;
                    }
                    //敌人列表
                    for (int i = 0; i < enemyList.Count; i++)
                    {
                        Enemy enemy = enemyList[i];
                        if (x == enemy.x && y == enemy.y)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkMagenta;
                            Console.Write(enemy.display);
                            goto w;
                        }
                    }
                    //宝箱列表
                    for (int i = 0; i < chestList.Count; i++)
                    {
                        Chest chest = chestList[i];
                        if (x == chest.x && y == chest.y)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkCyan;
                            Console.Write("θ");
                            goto w;
                        }
                    }
                    //BOSS
                    if (level == BOSS_LEVEL)
                    {
                        map[9, 8] = "△"; map[10, 8] = "△";
                        map[8, 9] = "ψ"; map[9, 9] = "╲"; map[10, 9] = "╱";
                        map[8, 10] = "｜"; map[9, 10] = "）"; map[10, 10] = "（"; map[11, 10] = "w ";
                        map[8, 11] = "｜"; map[9, 11] = "╱"; map[10, 11] = "╲";
                    }
                    //BOSS颜色渲染
                    if (level == BOSS_LEVEL && x >= 8 && x <= 11 && y >= 8 && y <= 11)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    else if (x == map.GetLength(0) - 2 && y == map.GetLength(1) - 1)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }

                    Console.Write(map[x, y]);
                w:;
                }
                Console.WriteLine();
            }
            if (isDead)
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                Console.WriteLine("□□□□□                    □□□□□");
                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");
                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□  □  □□□□  □  □□  □□");
                Console.WriteLine("□□  □□□  □□□□□□  □□□  □□");
                Console.WriteLine("□□  □□  □  □□□□  □  □□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□        □□□□□  □□");
                Console.WriteLine("□□  □□□□  □□□□  □□□□  □□");
                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");
                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");
                Console.WriteLine("□□□□□                    □□□□□");
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                return;
            }
            if (boss == null)
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                Console.WriteLine("□□□□□                    □□□□□");
                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");
                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□  □□□□□□  □□□  □□");
                Console.WriteLine("□□  □□  □  □□□□  □  □□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");
                Console.WriteLine("□□  □□□□  □□□□  □□□□  □□");
                Console.WriteLine("□□  □□□□□        □□□□□  □□");
                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");
                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");
                Console.WriteLine("□□□□□                    □□□□□");
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");
                return;
            }
            Console.SetCursorPosition(0, 0);
            Console.Write("第 " + level + " 层");
        }
        private static void MapGenerator()
        {
            level++;
            Player.x = 1; Player.y = 1;
            enemyList.Clear();
            chestList.Clear();
            for (int y = 0; y < map.GetLength(1); y++)
            {
                for (int x = 0; x < map.GetLength(0); x++)
                {
                    //边界
                    if (x == 0 || y == 0 || x == map.GetLength(0) - 1 || y == map.GetLength(1) - 1)
                    {
                        map[x, y] = "□";
                        if (x == map.GetLength(0) - 2 && y == map.GetLength(1) - 1 && level != BOSS_LEVEL)
                        {
                            map[x, y] = "︾";
                        }
                    }
                    else
                    {
                        if (level == BOSS_LEVEL)
                        {
                            map[x, y] = "  ";
                            if (y == 5 || y == 14)
                            {
                                if (x >= 5 && x <= 14 && x != 9 && x != 10)
                                {
                                    map[x, y] = "□";
                                }
                            }
                            else if (x == 14 || x == 5)
                            {
                                if (y >= 5 && y <= 14 && y != 9 && y != 10)
                                {
                                    map[x, y] = "□";
                                }
                            }
                            continue;
                        }
                        if (Event.rnd.Next(0, 8) == 0 && x != 1 && y != 1 && x != map.GetLength(0) - 2 && y != map.GetLength(1) - 2)
                        {
                            map[x, y] = "□";
                        }
                        else
                        {
                            map[x, y] = "  ";
                        }

                        //生成敌人
                        if (map[x, y] == "  ")
                        {
                            if (Event.rnd.Next(0, 20) == 0 && x != 1 && y != 1 && x != map.GetLength(0) - 2 && y != map.GetLength(1) - 2)
                            {
                                enemyList.Add(new Enemy(x, y));
                                continue;
                            }
                            if (Event.rnd.Next(0, 80) == 0 && x != 1 && y != 1 && x != map.GetLength(0) - 2 && y != map.GetLength(1) - 2)
                            {
                                chestList.Add(new Chest(x, y));
                                continue;
                            }
                        }
                    }
                }
            }
        }

        private static void Info()
        {
            Console.SetCursorPosition(55, 20);
            Console.WriteLine("Developed by Linus C.");
        }
    }
    internal class Event
    {
        public static Random rnd = new Random();
    }
    internal class Player
    {
        public static int lv = 1, exp = 0, ap = 3, x = 1, y = 1, life = 20, mana = 10;
        public static bool skill1 = false, skill2 = false, skill3 = false;
        private static int _atk, _def, _dex, _int;
        public static int atk { get => _atk; set => _atk = value; }
        public static int def { get => _def; set => _def = value; }
        public static int dex { get => _dex; set => _dex = value; }
        public static int Int { get => _int; set => _int = value; }
        public static void Reset()
        {
            lv = 1; exp = 0; ap = 3; x = 1; y = 1;
            life = 20; mana = 10;
            skill1 = false; skill2 = false; skill3 = false;
            _atk = 2; _def = 1; _dex = 1; _int = 0;
        }
        //受伤判断
        public static int GetHurt(int _atk, int _dex)
        {
            Random rnd = Event.rnd;
            _atk = _atk + rnd.Next(0, (int)(_atk * 0.5f));
            int mrate = 0;
            //对方敏捷-我方敏捷 
            int dex0 = _dex - dex;
            //如果对方敏捷高，从0~dex 为正值，否则 -dex~1
            if (dex0 > 0)
            {
                mrate = Event.rnd.Next(0, dex0);
            }
            else
            {
                mrate = Event.rnd.Next(dex0, 1);
            }
            //如果随机数为负数
            if (mrate < 0)
            {
                _atk = -1;
            }
            else if (mrate > 0)
            {
                _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);
            }
            //如果随机数为0 则1/2几率闪避
            else
            {
                if (Event.rnd.Next(0, 2) == 0)
                {
                    _atk = -1;
                }
                else
                {
                    _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);
                }
            }
            life -= _atk > 0 ? _atk : 0;
            return _atk;
        }
    }

    internal class Enemy
    {
        public string name;
        public char display;
        public int x, y, life, atk, def, dex, exp;
        public Enemy(int _x, int _y, bool isBoss = false)
        {
            x = _x;
            y = _y;
            int r = Event.rnd.Next(0, 6);
            if (isBoss)
            {
                r = 6;
            }

            switch (r)
            {
                case 0:
                    display = '鼠';
                    name = "老鼠";
                    life = 5;
                    atk = 2;
                    def = 1;
                    dex = 0;
                    exp = 1;
                    break;
                case 1:
                    display = '蝙';
                    name = "蝙蝠";
                    life = 4;
                    atk = 2;
                    def = 1;
                    dex = 2;
                    exp = 1;
                    break;
                case 2:
                    display = '蛛';
                    name = "蜘蛛";
                    life = 3;
                    atk = 1;
                    def = 0;
                    dex = 4;
                    exp = 1;
                    break;
                case 3:
                    display = '蛇';
                    name = "蛇";
                    life = 5;
                    atk = 3;
                    def = 1;
                    dex = 3;
                    exp = 2;
                    break;
                case 4:
                    display = '幽';
                    name = "幽灵";
                    life = 5;
                    atk = 3;
                    def = 4;
                    dex = 2;
                    exp = 2;
                    break;
                case 5:
                    display = '骷';
                    name = "骷髅";
                    life = 8;
                    atk = 4;
                    def = 3;
                    dex = 2;
                    exp = 3;
                    break;
                case 6:
                    display = '王';
                    name = "恶魔";
                    life = 30;
                    atk = 8;
                    def = 6;
                    dex = 5;
                    exp = 20;
                    break;
            }
        }
        public int GetHurt(int _atk, int _dex)
        {
            Random rnd = Event.rnd;
            _atk = _atk + rnd.Next(0, (int)(_atk * 0.5f));
            int mrate = 0;
            //对方敏捷-我方敏捷 
            int dex0 = _dex - dex;
            //如果对方敏捷高，从0~dex 为正值，否则 -dex~1
            if (dex0 > 0)
            {
                mrate = Event.rnd.Next(0, dex0);
            }
            else
            {
                mrate = Event.rnd.Next(dex0, 1);
            }
            //如果随机数为负数
            if (mrate < 0)
            {
                _atk = -1;
            }
            else if (mrate > 0)
            {
                _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);
            }
            //如果随机数为0 则1/2几率闪避
            else
            {
                if (Event.rnd.Next(0, 2) == 0)
                {
                    _atk = -1;
                }
                else
                {
                    _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);
                }
            }
            life -= _atk > 0 ? _atk : 0;
            return _atk;
        }
    }

    internal class Chest
    {
        public int x, y;
        public string treasure;
        public string[] treasureList = { "生命药水", "法力药水", "短剑", "盔甲", "火球术卷轴", "治愈术卷轴", "眩晕术卷轴" };
        public Chest(int _x, int _y)
        {
            x = _x;
            y = _y;
            int rnd = Event.rnd.Next(0, 185);
            if (rnd < 15)
            {
                treasure = treasureList[6];
            }
            else if (rnd >= 15 && rnd < 30)
            {
                treasure = treasureList[5];
            }
            else if (rnd >= 30 && rnd < 45)
            {
                treasure = treasureList[4];
            }
            else if (rnd >= 45 && rnd < 65)
            {
                treasure = treasureList[3];
            }
            else if (rnd >= 65 && rnd < 85)
            {
                treasure = treasureList[2];
            }
            else if (rnd >= 85 && rnd < 135)
            {
                treasure = treasureList[0];
            }
            else
            {
                treasure = treasureList[1];
            }
        }
    }

    #endregion

}